﻿using System.Xml.Linq;
using Engine;

namespace Game
{
    public class XCraftingRecipeWidget : CanvasWidget
    {
        public LabelWidget m_nameWidget;

        public LabelWidget m_descriptionWidget;

        public GridPanelWidget m_gridWidget;

        public XCraftingRecipeSlotWidget m_resultWidget;

        public CraftingRecipe m_recipe;

        public string m_nameSuffix;

        public bool m_dirty = true;

        public string NameSuffix
        {
            get
            {
                return m_nameSuffix;
            }
            set
            {
                if (value != m_nameSuffix)
                {
                    m_nameSuffix = value;
                    m_dirty = true;
                }
            }
        }

        public CraftingRecipe Recipe
        {
            get
            {
                return m_recipe;
            }
            set
            {
                if (value != m_recipe)
                {
                    m_recipe = value;
                    m_dirty = true;
                }
            }
        }

        public XCraftingRecipeWidget()
        {
            XElement node = ContentManager.Get<XElement>("Widgets/XCraftingRecipe");
            LoadContents(this, node);
            m_nameWidget = Children.Find<LabelWidget>("CraftingRecipeWidget.Name");
            m_descriptionWidget = Children.Find<LabelWidget>("CraftingRecipeWidget.Description");
            m_gridWidget = Children.Find<GridPanelWidget>("CraftingRecipeWidget.Ingredients");
            m_resultWidget = Children.Find<XCraftingRecipeSlotWidget>("CraftingRecipeWidget.Result");
            for (int i = 0; i < m_gridWidget.RowsCount; i++)
            {
                for (int j = 0; j < m_gridWidget.ColumnsCount; j++)
                {
                    XCraftingRecipeSlotWidget xCraftingRecipeSlotWidget = new XCraftingRecipeSlotWidget();
                    m_gridWidget.Children.Add(xCraftingRecipeSlotWidget);
                    xCraftingRecipeSlotWidget.Size = new Vector2(30f, 30f);
                    m_gridWidget.SetWidgetCell(xCraftingRecipeSlotWidget, new Point2(j, i));
                }
            }

            m_resultWidget.Size = new Vector2(30f, 30f);
        }

        public override void MeasureOverride(Vector2 parentAvailableSize)
        {
            if (m_dirty)
            {
                UpdateWidgets();
            }

            base.MeasureOverride(parentAvailableSize);
        }

        public void UpdateWidgets()
        {
            m_dirty = false;
            if (m_recipe != null)
            {
                Block block = BlocksManager.Blocks[Terrain.ExtractContents(m_recipe.ResultValue)];
                m_nameWidget.Text = block.GetDisplayName(null, m_recipe.ResultValue) + ((!string.IsNullOrEmpty(NameSuffix)) ? NameSuffix : string.Empty);
                m_descriptionWidget.Text = m_recipe.Description;
                m_nameWidget.IsVisible = true;
                m_descriptionWidget.IsVisible = true;
                foreach (XCraftingRecipeSlotWidget child in m_gridWidget.Children)
                {
                    Point2 widgetCell = m_gridWidget.GetWidgetCell(child);
                    child.SetIngredient(m_recipe.Ingredients[widgetCell.X + widgetCell.Y * 4]);
                }

                m_resultWidget.SetResult(m_recipe.ResultValue, m_recipe.ResultCount);
                return;
            }

            foreach (XCraftingRecipeSlotWidget child2 in m_gridWidget.Children)
            {
                child2.SetIngredient(null);
            }

            m_resultWidget.SetResult(0, 0);
        }
    }
}